package com.nt.train.service.impl;
import com.nt.train.entity.DictType;
import com.nt.train.service.DictTypeService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/17 13:55
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {

    private  static List<DictType> dbDictTypeList = null;

    static {
        dbDictTypeList = new ArrayList<DictType>();
        for(int i=1 ;i<= 50;i++){
            DictType dictType  = new DictType();
            dictType.setDictId(Long.valueOf(i));
            dictType.setDictName("北京-"+i);
            dictType.setDictType(""+(i%2));
            dictType.setStatus("0");
            //主键
            dbDictTypeList.add(dictType);
        }
    }


    @Override
    public List<DictType> listDictTypePage(Integer pageIndex, Integer pageSize, DictType DictType) {
        //总数
        int total = dbDictTypeList.size();
        //总页数
        int pageSum = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        //分页
        List<DictType> subList = dbDictTypeList.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
      //  subList = getPageList(pageIndex,pageSize,dbDictTypeList);
        return subList;
    }


    /**
     * 传统分页处理
     * @param pageNum
     * @param pageSize
     * @param l
     * @return
     */
    private static List<DictType> getPageList(Integer pageNum, Integer pageSize, List<DictType> l) {
        int count = 0;
        int fromIndex = 0;
        int toIndex = 0;
        if (l != null && l.size() > 0) {
            count = l.size();
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = pageNum * pageSize;
            if (toIndex > count) {
                toIndex = count;
            }
        }
        List<DictType> pageList = l.subList(fromIndex, toIndex);
        return pageList;
    }



    @Override
    public List<DictType> selectDictTypeList(DictType dictType) {
        List<DictType>  DictTypeList = dbDictTypeList.stream().filter(obj -> obj.getDictType().equals(dictType.getDictType())).collect(Collectors.toList());
        return DictTypeList;
    }

    @Override
    public List<DictType> selectDictTypeAll() {
        return dbDictTypeList;
    }



    @Override
    public DictType selectDictTypeById(Long dictTypeId) {
        //实现查找，找到则返回匹配的对象，找不到则返回null
        DictType DictTypeData = dbDictTypeList.stream().filter(DictType -> DictType.getDictId().equals(dictTypeId)) .findAny() .orElse(null);
        return DictTypeData;
    }

    @Override
    public DictType getByDictName(String dictName) {
        //实现查找，找到则返回匹配的对象，找不到则返回null
        DictType dictData = dbDictTypeList.stream().filter(dict -> dict.getDictName().equals(dictName)) .findAny() .orElse(null);
        return dictData;
    }


    @Override
    public void deleteDictTypeById(Long dictTypeId) {
       dbDictTypeList.stream().findFirst().map(DictType -> {
            if (DictType.getDictId().equals(dictTypeId)){
                dbDictTypeList.remove(DictType);
            }
            return DictType;
        });
        //System.out.println(dbDictTypeList.size());
    }

    @Override
    public void deleteDictTypeByIds(List<Long> ids) {
        ids.forEach(dictTypeId->{
            dbDictTypeList.stream().findFirst().map(DictType -> {
                if (DictType.getDictId().equals(dictTypeId)){
                    dbDictTypeList.remove(DictType);
                }
                return DictType;
            });
        });
    }

    @Override
    public int insertDictType(DictType dictTypeId) {
        dbDictTypeList.add(dictTypeId);
        return 1;
    }

    @Override
    public int updateDictType(DictType dictType) {
        dbDictTypeList.stream().filter(db->db.getDictId().equals(dictType.getDictId()))
                .forEach(f ->
                        {
                            f.setDictId((Long.valueOf(dictType.getDictId())));
                            f.setDictName(dictType.getDictName());
                            f.setDictType(dictType.getDictType());
                            f.setStatus("0");
                        }
                );
        return 1;
    }
}

